<?php


namespace app\spider\controller;
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");

use app\spider\controller\Curl as Curl;
use think\Db;

class Getcar
{

    /**
     * win: php index.php spider/getcar/getcarbrand
     * dev.ziwudiche.com/spider/getcar/getcarbrand
     */
    public function getCarBrand(){
        $curl=new Curl();
        $appkey = '62585d86dad50691';//你的appkey
        $url = "https://api.jisuapi.com/car/brand?appkey=$appkey";
        $result = $curl->curlOpen($url, ['ssl'=>true]);
        $jsonarr = json_decode($result, true);
        //exit(var_dump($jsonarr));
        if($jsonarr['status'] != 0)
        {
            echo $jsonarr['msg'];
            exit();
        }

        $result = $jsonarr['result'];
        foreach($result as $val)
        {
            $brand_data = [
                'oid' =>  $val['id'],
                'firstletter' => $val['initial'],
                'brand' => $val['name'],
                'ctime' => date('Y-m-d H:i:s',time()),
                'logo'=> $val['logo'],
            ];
            $id = Db::name('vehicle_brand')->insertGetId($brand_data);
        //  echo $val['id'].' '.$val['name'].' '.$val['initial'].' '.$val['parentid'].' '.$val['logo'].' '.$val['depth'].'<br>';
        }
    }

    /**
     * win: php index.php spider/getcar/getCarSeparation
     * dev.ziwudiche.com/spider/getcar/getsizetype
     */
    public function getsizetype(){
        $size=Db::name('vehicle_separation')->distinct(true)->field('sizetype')->select();
        foreach ($size as $v){
            echo $v['sizetype']. "<br>";
        }
    }
    /**
     * win: php index.php spider/getcar/test
     * dev.ziwudiche.com/spider/getcar/test
     */
    public function test(){
//      $result= Db::name('city')->where('id','>',1)->update(['grade'=>3]);
//      $result= Db::name('city')->where('grade',1)->select();
//      foreach ($result as $v){
//          var_dump($v['name']);
//      }
//        var_dump($result);
//        $str='东莞市深圳市宜昌市青岛市烟台市厦门市佛山市中山市南宁市十堰市襄阳市湘潭市株洲市扬州市台州市温州市苏州市';
//        $arr = str_split($str, 9);
//        foreach ($arr as $v){
//            $result= Db::name('city')->where('name',$v)->update(['grade'=>2]);
//
//        }
//        $result= Db::name('city')->where('grade',2)->select();
//        var_dump($result);
    }



    /**
     * win: php index.php spider/getcar/getCarSeparation
     * dev.ziwudiche.com/spider/getcar/getCarSeparation
     */
    public  function  getCarSeparation(){
        $brandoid=Db::name('vehicle_brand')->column('oid');
        foreach ($brandoid as $parentid){
            $curl=new Curl();
            $appkey = '62585d86dad50691';//你的appkey
        //            $parentid = 1;//品牌ID
            $url = "https://api.jisuapi.com/car/type?parentid=$parentid&appkey=$appkey";
            $result =$curl->curlOpen($url, ['ssl'=>true]);
            $jsonarr = json_decode($result, true);
        //exit(var_dump($jsonarr));
            if($jsonarr['status'] != 0)
            {
                echo $jsonarr['msg'];
                exit();
            }
            $result = $jsonarr['result'];
            foreach($result as $val)
            {
//            echo $val['id'].' '.$val['name'].' '.$val['initial'].'<br><br>';//车子公司信息，2级
                if(!empty($val['list'])){
                    foreach($val['list'] as $v)
                    {
                        $separation_data = [
                            'oid' =>  $v['id'],
                            'pid' => $parentid,
                            'salestate' => $v['salestate'],
                            'separation' =>$v['fullname'],
                            'ctime' => date('Y-m-d H:i:s',time()),
                        ];
                        dump($parentid);
                    $separationres = Db::name('vehicle_separation')->insert($separation_data);
//                echo $v['id'].' '.$v['name'].' '.$v['fullname'].' '.$v['logo'].' '.$v['salestate'].' '.$v['depth'].'<br>';//车信息，3级
                    }
                }
            }
        }
    }


    /**
     * win: php index.php spider/getcar/getCarType
     * dev.ziwudiche.com/spider/getcar/getCarType
     */
    public function getCarType(){
        $separationoid=Db::name('vehicle_separation')->column('oid');
//        echo "<pre>";
//        die(var_dump( $separationoid));
//        echo "</pre>";
        foreach ($separationoid as $parentid){
            $curl=new Curl();

            $appkey = '62585d86dad50691';//你的appkey
//        $parentid = 220;//品牌ID
            $url = "https://api.jisuapi.com/car/car?parentid=$parentid&appkey=$appkey";
            $result =$curl->curlOpen($url, ['ssl'=>true]);
            $jsonarr = json_decode($result, true);
//exit(var_dump($jsonarr));
            if($jsonarr['status'] != 0)
            {
                echo $jsonarr['msg'];
                exit();
            }

            $result = $jsonarr['result'];

//        echo $result['id'].' '.$result['name'].' '.$result['initial'].' '.$result['fullname'].' '.$result['depth'].' '.$result['logo'].' '.$result['salestate'].'<br>';//车信息，3级

            if(!empty($result['list'])){
                $sizetype=$result['list'][0]['sizetype'];

                Db::name('vehicle_separation')->where('oid', $parentid)->update(['sizetype' => $sizetype]);
                foreach($result['list'] as $vv)
                {
                    $price=floatval($vv['price'])*10000;
                    $type_data = [
                        'oid' => $vv['id'],
                        'pid' => $parentid,
                        'year' =>$vv['yeartype'],
                        'type' =>$vv['name'],
                        'price' =>$price,
                        'productionstate'=>$vv['productionstate'],
                        'salestate'=>$vv['salestate'],
                        'ctime' => date('Y-m-d H:i:s',time()),
                     ];

                    $types = Db::name('vehicle_type')->insertGetId($type_data);
                    var_dump($types);
//                    echo $vv['id'].' '.$vv['name'].' '.$vv['logo'].' '.$vv['price'].' '.$vv['yeartype'].' '.$vv['productionstate'].' '.$vv['salestate'].' '.$vv['sizetype'].'<br>';//车信息，4级
                }
            }

        }


    }

}